package com.situ.mall.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.situ.mall.common.entity.Category;
import com.situ.mall.common.entity.Product;
import com.situ.mall.common.mapper.CategoryMapper;
import com.situ.mall.common.mapper.ProductMapper;
import com.situ.mall.common.service.IProductService;
import com.situ.mall.common.vo.EditProductVO;
import com.situ.mall.common.vo.IndexProductVO;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kkkkkk
 * @since 2023-09-12
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public TableVO selectList(Integer page, Integer limit, String name, String status) {
        PageHelper.startPage(page, limit);
        return TableVO.data(baseMapper.list(name, status));
    }

    @Override
    public void delete(String ids) {
        baseMapper.delete2(ids.split(","));
    }

    @Override
    public EditProductVO edit(Integer id) {
        return baseMapper.edit(id);
    }

    @Override
    //接收的参数是一级分类的ID　　比如手机传：1
    public List<IndexProductVO> indexProducts(Integer parentId) {
        List<IndexProductVO> result = new ArrayList<>();
        List<Category> list = categoryMapper.select8(parentId);
        for (Category c : list) {
            IndexProductVO p = baseMapper.selectCheap(c.getId());
            if (p != null) {
                p.setSlogan(c.getTitle());
                result.add(p);
            }
        }
        return result;
    }
}
